﻿using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace T.SF.Console
{
    internal class ConfigCenterManager : IProviderConfigManager
    {

        private static string configCenterServer = string.Empty;
        public ConfigCenterManager()
        {
            if (string.IsNullOrEmpty(configCenterServer))
                configCenterServer = GetConfigCenter();
        }

        public string GetConfigCenter()
        {
            string zookeeperStr = ConfigurationManager.AppSettings.Get("ConfigCenter.Url");

            if (string.IsNullOrEmpty(zookeeperStr))
                zookeeperStr = "http://localhost/Config";

            return zookeeperStr;
        }

        /// <summary>
        /// 只允许外部在程序启动时调用一次
        /// </summary>
        /// <param name="configCenterUrl"></param>
        internal static void SetConfigCenterUrl(string configCenterUrl)
        {
            if (!string.IsNullOrWhiteSpace(configCenterUrl) && string.IsNullOrWhiteSpace(configCenterServer))
            {
                configCenterServer = configCenterUrl;
            }
        }

        private string GetData(string key)
        {
            using (var httpClient = new HttpClient())
            {
                httpClient.Timeout = new TimeSpan(0, 0, 10);
                httpClient.BaseAddress = new Uri(configCenterServer);
                using (var response = httpClient.GetAsync("API/Get?key=" + key).Result)
                {
                    response.EnsureSuccessStatusCode();
                    var result = response.Content.ReadAsStringAsync().Result;
                    var jt = JObject.Parse(result);
                    if (jt["result_code"].ToString() == "0")
                    {
                        return jt["result_content"].ToString();
                    }

                    return null;
                }
            }
        }

        public long GetHeartBeatTimeSpan()
        {
            string zookeeperStr = GetData("TTP.HSF.HeartBeatTimeSpan");

            if (string.IsNullOrEmpty(zookeeperStr))
                zookeeperStr = "10000";

            long result = 10000;
            long.TryParse(zookeeperStr, out result);

            return result;
 
        }

        public int GetMaxWorkerThreads()
        {
            string zookeeperStr = GetData("TTP.HSF.Max.WorkerThreads");

            if (string.IsNullOrEmpty(zookeeperStr))
                zookeeperStr = "1000";

            int result = 1000;
            int.TryParse(zookeeperStr, out result);

            return result;

        }

        public int GetMaxCompletionPortThreads()
        {
            string zookeeperStr = GetData("TTP.HSF.Max.CompletionPortThreads");

            if (string.IsNullOrEmpty(zookeeperStr))
                zookeeperStr = "200";

            int result = 200;
            int.TryParse(zookeeperStr, out result);

            return result;

        }

        public int GetMinWorkerThreads()
        {
            string zookeeperStr = GetData("TTP.HSF.Min.WorkerThreads");

            if (string.IsNullOrEmpty(zookeeperStr))
                zookeeperStr = "100";

            int result = 100;
            int.TryParse(zookeeperStr, out result);

            return result;

        }

        public int GetMinCompletionPortThreads()
        {
            string zookeeperStr = GetData("TTP.HSF.Min.CompletionPortThreads");

            if (string.IsNullOrEmpty(zookeeperStr))
                zookeeperStr = "20";

            int result = 20;
            int.TryParse(zookeeperStr, out result);

            return result;

        }

        public int GetFusingTPM()
        {
            string zookeeperStr = GetData("TTP.HSF.Fusing.MaxTPM");

            if (string.IsNullOrEmpty(zookeeperStr))
                zookeeperStr = "200";

            int result = 200;
            int.TryParse(zookeeperStr, out result);

            return result;
        }

        public int GetFusingCount()
        {
            string zookeeperStr = GetData("TTP.HSF.Fusing.Count");

            if (string.IsNullOrEmpty(zookeeperStr))
                zookeeperStr = "2";

            int result = 2;
            int.TryParse(zookeeperStr, out result);

            return result;
        }

        public int GetFusingTime()
        {
            string zookeeperStr = GetData("TTP.HSF.Fusing.Time");

            if (string.IsNullOrEmpty(zookeeperStr))
                zookeeperStr = "10";

            int result = 10;
            int.TryParse(zookeeperStr, out result);

            return result;
        }

    }
}
